<template>
  <div class="goods-comment" v-if="commentInfo">
    <!-- 头部 -->
    <div class="head">
      <div class="data">
        <p><span>{{ commentInfo.salesCount }}</span><span>人购买</span></p>
        <p><span>{{ commentInfo.praisePercent }}</span><span>好评率</span></p>
      </div>
      <div class="tags">
        <div class="dt">大家都在说：</div>
        <div class="dd">
          <a @click="changeTag(i)" v-for="(item, i) in commentInfo.tags" :key="i" href="javascript:;"
            :class="{ active: currentTagIndex === i }">{{
              item.title }}（{{ item.tagCount }}）</a>
        </div>
      </div>
    </div>
    <!-- 排序筛选 -->
    <div class="sort">
      <span>排序：</span>
      <a @click="changeSort(null)" href="javascript:;" :class="{ active: currentSort === null }">默认</a>
      <a @click="changeSort('createTime')" :class="{ active: currentSort === 'createTime' }" href="javascript:;">最新</a>
      <a @click="changeSort('praiseCount')" :class="{ active: currentSort === 'praiseCount' }" href="javascript:;">最热</a>
    </div>
    <!-- 评价列表 -->
    <div class="list" v-if="commentList">
      <div class="item" v-for="item in commentList" :key="item.id">
        <div class="user">
          <img :src="item.member.avatar" alt="">
          <span>{{ formatNickname(item.member.nickname) }}</span>
        </div>
        <div class="body">
          <div class="score">
            <i v-for="i in item.score" :key="i" class="iconfont icon-wjx01"></i>
            <i v-for="i in (5 - item.score)" :key="i" class="iconfont icon-wjx02"></i>
            <span class="attr">{{ formatSpecs(item.orderInfo.specs) }}</span>
          </div>
          <div class="text">{{ item.content }}</div>
          <!-- 图片预览组件 -->
          <GoodsCommentImage v-if="item.pictures.length" :pictures="item.pictures" />
          <div class="time">
            <span>{{ item.createTime }}</span>
            <span class="zan"><i class="iconfont icon-dianzan"></i>{{ item.praiseCount }}</span>
          </div>
        </div>
      </div>
    </div>
    <!-- endList -->
    <!-- 分页 -->
    <XtxPagination :total="total" :currentPage="reqParams.page" :pageSize="reqParams.pageSize"
      @change-current="changePager"></XtxPagination>
  </div>
</template>
<script>
import GoodsCommentImage from './goods-comment-image.vue'
import { findCommentInfoByGoods, findGoodsCommentList } from '@/api/product'
import { inject, reactive, ref, watch } from 'vue'
export default {
  name: 'GoodsComment',
  components: { GoodsCommentImage },
  setup() {
    const goods = inject('goods')
    // 定义评价数据
    const commentInfo = ref(null)
    findCommentInfoByGoods(goods.value.id).then(({ result }) => {
      // 有图
      result.tags.unshift({ type: 2, title: '有图', tagCount: result.hasPictureCount })
      // 全部评价
      result.tags.unshift({ type: 1, title: '全部评价', tagCount: result.evaluateCount })
      commentInfo.value = result
    })
    // 定义默认选中的tag索引
    const currentTagIndex = ref(0)
    // 更改tag
    const changeTag = (index) => {
      // 选中的样式
      currentTagIndex.value = index
      // 改变筛选条件
      // 1.点击全部评价
      // 2.点击有图
      // 3.点击其他标签
      const currentTag = commentInfo.value.tags[index]
      if (currentTag.type === 1) {
        // 全部
        reqParams.hasPicture = null
        reqParams.tag = null
      } else if (currentTag.type === 2) {
        // 有图
        reqParams.hasPicture = true
        reqParams.tag = null
      } else {
        // 其他标签
        reqParams.hasPicture = null
        reqParams.tag = currentTag.title
      }
      // 改变筛选条件了,页码都从第1页开始
      reqParams.page = 1
    }
    // 初始化或者筛选条件改变后，获取列表数据。
    const commentList = ref([])
    // 总记录数
    const total = ref(0)
    // 定义一个筛选条件的数据
    const reqParams = reactive({
      page: 1, // 当前页
      pageSize: 10, // 每页显示的条数
      hasPicture: null, // 是否选择了有图的tag,如果选择了，那么设置值为true,没选择设置为null,null数据的条件在发送请求时会自动过滤掉
      tag: null,
      sortField: null
    })
    // 侦听请求参数的变化去发起网络请求获取评价列表的数据
    watch(reqParams, () => {
      findGoodsCommentList(goods.value.id, reqParams).then(data => {
        commentList.value = data.result.items
        total.value = data.result.counts
      })
    }, { immediate: true })

    // 定义转换数据的函数（对应vue2.0的过滤器）
    const formatSpecs = (specs) => {
      return specs.reduce((p, c) => `${p} ${c.name}：${c.nameValue}`, '').trim()
    }
    const formatNickname = (nickname) => {
      return nickname[0] + '****' + nickname.substr(-1)
    }

    // 排序筛选
    const currentSort = ref(null)
    const changeSort = (sortField) => {
      currentSort.value = sortField
      reqParams.sortField = sortField
      // 条件改变时从第一页开始请求数据
      reqParams.page = 1
    }

    // 接收子组件传递来的页码
    const changePager = (page) => {
      reqParams.page = page
    }

    return { commentInfo, currentTagIndex, changeTag, commentList, reqParams, formatSpecs, formatNickname, currentSort, changeSort, total, changePager }
  }
}
</script>
<style scoped lang="less">
.goods-comment {
  .head {
    display: flex;
    padding: 30px 0;

    .data {
      width: 340px;
      display: flex;
      padding: 20px;

      p {
        flex: 1;
        text-align: center;

        span {
          display: block;

          &:first-child {
            font-size: 32px;
            color: @priceColor;
          }

          &:last-child {
            color: #999;
          }
        }
      }
    }

    .tags {
      flex: 1;
      display: flex;
      border-left: 1px solid #f5f5f5;

      .dt {
        font-weight: bold;
        width: 100px;
        text-align: right;
        line-height: 42px;
      }

      .dd {
        flex: 1;
        display: flex;
        flex-wrap: wrap;

        >a {
          width: 132px;
          height: 42px;
          margin-left: 20px;
          margin-bottom: 20px;
          border-radius: 4px;
          border: 1px solid #e4e4e4;
          background: #f5f5f5;
          color: #999;
          text-align: center;
          line-height: 40px;

          &:hover {
            border-color: @xtxColor;
            background: lighten(@xtxColor, 50%);
            color: @xtxColor;
          }

          &.active {
            border-color: @xtxColor;
            background: @xtxColor;
            color: #fff;
          }
        }
      }
    }
  }

  .sort {
    height: 60px;
    line-height: 60px;
    border-top: 1px solid #f5f5f5;
    border-bottom: 1px solid #f5f5f5;
    margin: 0 20px;
    color: #666;

    >span {
      margin-left: 20px;
    }

    >a {
      margin-left: 30px;

      &.active,
      &:hover {
        color: @xtxColor;
      }
    }
  }

  .list {
    padding: 0 20px;

    .item {
      display: flex;
      padding: 25px 10px;
      border-bottom: 1px solid #f5f5f5;

      .user {
        width: 160px;

        img {
          width: 40px;
          height: 40px;
          border-radius: 50%;
          overflow: hidden;
        }

        span {
          padding-left: 10px;
          color: #666;
        }
      }

      .body {
        flex: 1;

        .score {
          line-height: 40px;

          .iconfont {
            color: #ff9240;
            padding-right: 3px;
          }

          .attr {
            padding-left: 10px;
            color: #666;
          }
        }
      }

      .text {
        color: #666;
        line-height: 24px;
      }

      .time {
        color: #999;
        display: flex;
        justify-content: space-between;
        margin-top: 5px;
      }
    }
  }
}
</style>